package com.code.wordhelper.model;

import lombok.Data;

@Data
public class Result<T> {
    private static final long serialVersionUID = 1L;
    private String code;
    private T body;
    private String messages;

    public Result(String rtnCode, T body, String messages) {
        this.code = ResultCode.SUCCESS.getCode();
        this.messages = messages;
        this.code = rtnCode;
        this.body = body;
    }

    public Result() {
    }

    public Result(String rtnCode, T body) {
        this(rtnCode, body, null);
    }

    public Result(String rtnCode) {
        this(rtnCode, (T) null);
    }

    public Result(T body) {
        this(ResultCode.SUCCESS.getCode(), body);
    }

    public static <R> Result failure(String errorCode) {
        return new Result(errorCode);
    }

    public static <R> Result failure(String errorCode, String message) {
        Result entity = new Result(errorCode);
        entity.messages = message;
        return entity;
    }

    public static <R> Result failure(ResultCode resultCode) {
        Result entity = new Result(resultCode.getCode());
        entity.messages = resultCode.getMessage();
        return entity;
    }

    public static <R> Result<R> ok() {
        return new Result(ResultCode.SUCCESS.getCode());
    }

    public static <R> Result<R> ok(R value) {
        Result response = new Result(ResultCode.SUCCESS.getCode());
        response.setBody(value);
        return response;
    }

    public static <R> Result ok(R value, String message) {
        Result response = new Result(ResultCode.SUCCESS.getCode());
        response.setBody(value);
        response.setMessages(message);
        return response;
    }
}
